package com.atguigu.custmersystem.customerview;

import com.atguigu.custmersystem.javabean.Customer;
import com.atguigu.custmersystem.service.CustomerService;
import com.atguigu.custmersystem.tools.CMUtility;

/**
 * @version 1.0
 * @Author 马冰
 * @Date 2024/11/11 14:49
 * @注释 电商管理系统视图层展示
 */
public class CustomerView {

    CustomerService service = new CustomerService(5);

    public static void main(String[] args) {
        CustomerView cv = new CustomerView();
        cv.enterMainMenu();
    }

    public void enterMainMenu() {
        boolean loopFlag = true;
        while (loopFlag) {
            System.out.print("""
                                 -----------------拼电商客户管理系统-----------------
                                        
                                                  1 添 加 客 户
                                                  2 修 改 客 户
                                                  3 删 除 客 户
                                                  4 客 户 列 表
                                                  5 退       出
                                 
                                                  请选择(1-5)：""");
            char select = CMUtility.readMenuSelection();
            switch (select) {
                case '1':
                    addNewCustomer();
                    break;
                case '2':
                    modifyCustomer();
                    break;
                case '3':
                    deleteCustomer();
                    break;
                case '4':
                    listAllCustomer();
                    break;
                case '5':
                    System.out.print("确认退出吗(Y/N)：");
                    char c = CMUtility.readConfirmSelection();
                    if (c == 'Y' || c == 'y') {
                        System.out.println("欢迎下次光临！");
                        loopFlag = false;
                    }
                    break;

            }
        }
    }

    /**
     * 添加新客户
     */
    private void addNewCustomer() {
        System.out.println("---------------添加客户-------------");
        //输入新增客户信息
        System.out.print("姓名：");
        String name = CMUtility.readString(4);
        System.out.print("性别：");
        char gender = CMUtility.readChar();
        System.out.print("年龄：");
        int age = CMUtility.readInt();
        System.out.print("电话：");
        String phone = CMUtility.readString(11);
        System.out.print("邮箱：");
        String email = CMUtility.readString(20);

        //将客户信息进行封装
        Customer customer = new Customer(name, gender, age, phone, email);
        //和服务端进行链接
        service.addCustomer(customer);
    }

    /**
     * 修改客户
     */
    public void modifyCustomer() {
        System.out.println("----------------------修改客户----------------------");
        System.out.println("请选择待修改客户编号(-1退出)：");
        int id = CMUtility.readInt();
        //获取待修改客户信息
        Customer customer = service.getCustomer(id - 1);
        //对客户信息进行修改
        System.out.print("姓名(" + customer.getName() + ")：");
        String name = CMUtility.readString(20, customer.getName());
        System.out.print("性别(" + customer.getGender() + ")：");
        char gender = CMUtility.readChar(customer.getGender());
        System.out.print("年龄(" + customer.getAge() + ")：");
        int age = CMUtility.readInt(customer.getAge());
        System.out.print("电话(" + customer.getPhone() + ")：");
        String phone = CMUtility.readString(11, customer.getPhone());
        System.out.print("邮箱(" + customer.getEmail() + ")：");
        String email = CMUtility.readString(20, customer.getEmail());
        //对修改后的信息进行封装
        Customer customer1 = new Customer(name, gender, age, phone, email);
        //对封装后的信息进行赋值修改
        boolean b = service.replaceCustomer(id - 1, customer1);
        if (b) {
            System.out.println("修改完成");
        } else {
            System.out.println("修改失败");
        }

        System.out.println("----------------------修改完成----------------------");
    }

    /**
     * 删除用户
     */
    public void deleteCustomer() {
        System.out.println("---------------------删除客户---------------------");
        System.out.print("请选择待删除客户编号(-1退出)：");
        int id = CMUtility.readInt();
        if (id == -1) {
            return;
        }
        System.out.println("请确认是否删除（Y/N）:");
        char select = CMUtility.readConfirmSelection();
        if (select == 'Y' || select == 'y') {
            boolean b = service.deleteCustomer(id - 1);
            if (b) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        }

    }

    /**
     * 客户列表
     */
    public void listAllCustomer() {
        System.out.println("------------------客户列表-------------------");

        System.out.println("编号\t姓名\t\t性别\t\t年龄\t\t电话\t\t\t\t邮箱");
        Customer[] customer = service.getAllCustomers();
        for (int i = 0; i < customer.length; i++) {
            System.out.println((i + 1) + "\t" + customer[i]);
        }
        System.out.println("------------------客户列表完成-------------------");
    }
}
